package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.code.dao.TeacherMapper;
import com.code.dao.StudentMapper;
import com.code.entity.PageResult;
import com.code.entity.QueryPageBean;
import com.code.pojo.Teacher;
import com.code.pojo.Student;
import com.code.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    TeacherMapper productMapper;


    @Override
    public PageResult<Student> findPage(QueryPageBean queryPageBean) {
     PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Student> studentList = studentMapper.findLike(queryPageBean);
        PageInfo<Student> pageInfo = new PageInfo<>(studentList);
        PageResult<Student> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;

    }
//    @Override
//    public PageResult<Student> findPage(Student student) {
//     PageHelper.startPage(student.getPage(),student.getSize());
//        List<Student> studentList = studentMapper.findLike(student);
//        PageInfo<Student> pageInfo = new PageInfo<>(studentList);
//        PageResult<Student> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
//        return result;
//    }



    @Override
    public Student findById(Integer id) {
        return studentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    public void delete(Integer id) {
        studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Teacher> findTeacher() {
        return productMapper.selectAll();
    }


//   统计用注解写Sql语句 字段名称为 name  value
    @Override
    public List<Map> tongji() {
        return studentMapper.tongji();
    }

//    @Override
//    public List<Map> twobzt() {
//        return studentMapper.twobzt();
////        return null;
//    }

    @Override
    public PageResult<Student> findPageNo(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Student> studentList = studentMapper.findPageNo(queryPageBean);
        PageInfo<Student> pageInfo = new PageInfo<>(studentList);
        PageResult<Student> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;
    }

    @Override
    public void editState(Integer id) {
        Student student = studentMapper.selectByPrimaryKey(id);
        student.setState(1);
        studentMapper.updateByPrimaryKeySelective(student);
    }
}
